package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.Kcgl;
import top.jnaw.jee.platform.model.WechatUsers;
import top.jnaw.jee.utils.EnDeCode;
import top.jnaw.jee.utils.Https;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;
import top.jnaw.jee.utils.Strings;
import top.jnaw.jee.utils.nim.NIMSMS;
import top.jnaw.jee.utils.wechat.Wechats;

public class WechatController extends Controller {

  private static final String WECHAT_STATE = "wechat";
  private static final String WECHAT_APP_URL =
      Consts.DOMAIN + "wechat/index.html?data=%s";

  public void indexRediret() {

    Wechats.redirect(this, WECHAT_STATE, WECHAT_APP_URL);
  }

  public void auth() {
    JSONObject req = Strings.toJSONObject(this);
    final String code = Strings.getString(req, "code", "");
    final String state = Strings.getString(req, "state", "");
    Log.d("code::" + code);
    Log.d("state::" + state);
    JSONObject info = Wechats.getAuthInfo(code, state);
    String data;
    if (null == info) {
      renderError(Https.CODE_NOT_ACCEPTABLE);
      return;
    } else {
      ModelResult r = WechatUsers.save(info);
      Log.w(r.fine() + ", " + r.toJsonWithDateTime());

      JSONObject object = new JSONObject();
      object.put("openid", info.getString(Consts.OPENID));
      object.put("model", state.replace("0", ""));
      data = EnDeCode.enCodeBase64(object.toString());
    }
    Log.e("url" + String.format(info.getString("url"), data));
    redirect(String.format(info.getString("url"), data));
  }

  public void signature() {
    JSONObject req = Strings.toJSONObject(this);
    final String url = Strings.getString(req, "url", "");

    if (StrKit.isBlank(url)) {
      renderJson(Models.toJson(Result.kBadParam));
      return;
    }

    JSONObject data = Wechats.genSignedTicket(url);
    renderJson(Models.toJsonObject(data, Result.kSuccess));
  }

  public void login() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String name = Strings.getString(req, "name", "");
    final String mobile = Strings.getString(req, "mobile", "");
    final String captcha = Strings.getString(req, "captcha", "");

    if (NIMSMS.verifyCaptcha(mobile, captcha)) {
      result = WechatUsers.login(name, mobile);
    } else {
      result.setResult(Result.kBadCaptcha);
    }
    renderJson(result.toJson());
  }

  public void bind() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String openid = Strings.getString(req, "openid", "");
    final String name = Strings.getString(req, "name", "");
    final String mobile = Strings.getString(req, "mobile", "");
    final String captcha = Strings.getString(req, "captcha", "");

    if (NIMSMS.verifyCaptcha(mobile, captcha)) {
      result = WechatUsers.bind(openid, name, mobile);
    } else {
      result.setResult(Result.kBadCaptcha);
    }
    renderJson(result.toJson());
  }

  public void is_bind() {
    ModelResult result = new ModelResult(Result.kBadParam);
    JSONObject req = Strings.toJSONObject(this);
    final String openid = Strings.getString(req, "openid", "");
    if (StrKit.notBlank(openid)) {
      JSONObject object = new JSONObject();
      object.put("is_bind", WechatUsers.isBind(openid));
      result.setObject(object);
      result.setResult(Result.kSuccess);
    }
    renderJson(result.toJson());
  }

  public void list() {
    JSONObject req = Strings.toJSONObject(this);
    final int page = Strings.getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings.getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    final String keyWord = Strings.getString(req, "keyWord", "");
    renderJson(Strings
        .formatJsonDateTime(
            Strings.formatPage(WechatUsers.list(page, size, keyWord))));
  }

}
